package com.company.ecis.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.GifCaptcha;
import cn.hutool.core.net.Ipv4Util;
import cn.hutool.extra.servlet.ServletUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @author : 杨宇
 * @date : 2022-08-04 11:55
 **/
@Controller
@RequestMapping("/captcha")
public class CaptchaController {

    @RequestMapping("/getCaptcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
        GifCaptcha gifCaptcha = CaptchaUtil.createGifCaptcha(200,100);
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            // 将验证码code 存入 session 以访问ip命名
            request.getSession().setAttribute(ServletUtil.getClientIP(request),gifCaptcha.getCode());
            System.out.println(request.getSession().getAttribute(ServletUtil.getClientIP(request)));
            gifCaptcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
